from helpers.director.shortcut import TablePage,ModelTable,ModelFields,director,page_dc,get_request_cache
from .models import Userinfo,DepartMent
from helpers.case.jb_admin.admin_user import UserPage,UserFields,User
from django.db.models import F

class CusUserPage(UserPage):
    class tableCls(UserPage.tableCls):
        fields_sort = ['username','first_name','department','is_superuser','is_staff','is_active','groups','last_login']
        
        def get_operations(self):
            ops = super().get_operations()
            out =[]
            for op in ops:
                if op['name'] != 'delete_selected':
                    out.append(op)
            return out
        
        def getExtraHead(self):
            return [
                #{'name':'name','label':'姓名','editor':'com-table-span','width':150},
                {'name':'department','label':'部门','editor':'com-table-label-shower','width':100,},
            ]
        
        def inn_filter(self, query):
            query = super().inn_filter(query)
            return query.select_related('userinfo__department')
        
        def get_head_context(self):
            ctx = super().get_head_context()
            get_request_cache()['named_ctx'].update({
                'jb-user-tabs':[
                    {'name':'account',
                    'label':'基本信息',
                     'editor':'com-tab-form',
                     'mounted_express':'ex.vueAssign(scope.vc.row,scope.par_row)',
                     'fields_ctx':EasyJobUserForm().get_head_context()}
                ]
            })
            return ctx
       
        def dict_row(self, inst):
            
            return {
                'name':str(getattr( inst,'first_name','') ),
                'department': inst.userinfo.department.pk if inst.userinfo.department else None , # str(getattr(inst,'workinfo',''))
                '_department_label':str(inst.userinfo.department) if inst.userinfo.department else ''
            }

        

class EasyJobUserForm(UserFields):
    class Meta:
        model=User
        fields=['username','first_name','is_active','is_staff','is_superuser', 'date_joined','groups']
    
    def getExtraHeads(self):
        heads = super().getExtraHeads()
        heads+= [
            {'name':'department','label':'部门','required':True,'editor':'com-field-select','options':[
                {'value':x.pk,'label':x.name} for x in DepartMent.objects.all()
            ]}
        ]
        return heads
    
    def dict_row(self, inst):
        if getattr(inst,'userinfo',None):  
            return {
                'department': inst.userinfo.department.pk if inst.userinfo.department else None ,
                '_department_label':str(inst.userinfo.department) if inst.userinfo.department else ''            
            }
        else:
            return {}
    
    def clean_save(self):
        super().clean_save()
        if self.kw.get('department'):
            self.instance.userinfo.department_id = self.kw.get('department')
        else:
            self.instance.userinfo.department = None
        self.instance.userinfo.save()
        self.instance.userinfo.refresh_from_db()
    

#class UserInfoPage(TablePage):
    #def get_label(self):
        #return '用户信息'
    
    #def get_template(self, prefer=None):
        #return 'jb_admin/table_new.html'
    
    #class tableCls(ModelTable):
        #model = Userinfo
        #exclude =[]
        #pop_edit_fields =['id']

#class UserinfoForm(ModelFields):
    #class Meta:
        #model = Userinfo
        #exclude =[]

director.update({
    'userinfo':CusUserPage.tableCls,
    'userinfo.edit':EasyJobUserForm
})

page_dc.update({
    'userinfo':CusUserPage
})